package com.docking.heylo.service.impl;

import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.AndroidNotification;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;
import cn.jpush.api.push.model.notification.WinphoneNotification;
import com.docking.heylo.constant.SystemConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
@Configuration
public class JPushServiceImpl {


    @Value("${jpush.appKey}")
    private String appKey;

    @Value("${jpush.masterSecret}")
    private String masterSecret;

    @Bean
    public JPushClient createJPushClient(){
        return new JPushClient(masterSecret, appKey);
    }

    @Autowired
    private JPushClient jPushClient;


    /***
     * 发送系统通知
     *
     * @param userId            用户ID
     * @param title             标题
     * @param content           提示内容
     * @param params            提交参数
     * @return
     */
    public boolean notice(int userId, String title, String content, Map<String, String> params){
        return pushDecorateInfo(getAliasName(userId), title, content,params);
    }

    /***
     * 发送系统通知
     *
     * @param userIds           用户ID
     * @param title             标题
     * @param content           提示内容
     * @param params            提交参数
     * @return
     */
    public boolean notice(List<Integer> userIds, String title, String content, Map<String, String> params){
        return pushDecorateInfo(formatUserIds(userIds), title, content,params);
    }


    public String formatUserIds(List<Integer> userIds){
        StringBuffer sb = new StringBuffer();
        for(int userId : userIds){
            sb.append(getAliasName(userId)).append(",");
        }
        sb.deleteCharAt(sb.length() - 1);
        return sb.toString();
    }







    /***
     * 发送系统通知
     * @param content           通知内容
     */
    public boolean noticeAll(String content) {
        return pushDecorateInfo("all","通知",content,new HashMap<String, String>());
    }



    public boolean pushDecorateInfo(String alias, String title, String content, Map<String, String> params){
        try {
            PushPayload payload = PushPayload
                    .newBuilder()
                    .setPlatform(Platform.all())
                    .setAudience("all".equals(alias) ? Audience.all() : Audience.alias(alias))
                    .setNotification(
                            Notification
                                    .newBuilder()
                                    .addPlatformNotification(
                                            IosNotification.newBuilder()
                                                    .setAlert(content)
                                                    .setSound("happy.caf").setBadge(1)
                                                    .addExtras(params).build())
                                    .addPlatformNotification(
                                            AndroidNotification.newBuilder()
                                                    .setAlert(content)
                                                    .setTitle(title)
                                                    .addExtras(params).build())
                                    .addPlatformNotification(
                                            WinphoneNotification.newBuilder()
                                                    .setAlert(content)
                                                    .addExtras(params).build())
                                    .build()).setOptions(Options.newBuilder()
                            .setApnsProduction(false)
                            .build()).build();

            PushResult result = jPushClient.sendPush(payload);
            return result.isResultOK();
        } catch (APIConnectionException e) {
            e.printStackTrace();
        } catch (APIRequestException e) {
            e.printStackTrace();
        }
        return false;
    }




    /***
     * 获取用户极光别名
     *
     * @param userId
     * @return
     */
    public String getAliasName(int userId){
        return String.format(SystemConstant.JPush_alias_tpl,userId);
    }


    /***
     *
     * 确定发送通知的业务场景
     * 1.订单状态 [新订单、摄影师接单、模特支付订单、订单开始、订单结束]
     * 2.大A申请 通过
     * 3.好友动态
     *
     */

}
